$nolist
$include(C:\Users\Jonas\Documents\Freqgen\Include\reg832.PDF)
$list

;code adresdeclaraties
codestart   equ   0000h
timer1_int  equ   001bh
timer0_int	equ   000bh
Fadderl     equ   20h
Fadderm     equ   21h
Fadderh     equ   22h
Fincl       equ   23h
Finch       equ   24h
;declaratie van data-waarden
stack       equ   07fh


            org   codestart         ;reset vector
            ljmp  start
            org   timer0_int        ;timer1 int
            ljmp  timer_143us       ;timeroverflow om de 143us

start:
            mov   sp,#stack
;systeemfreq init
            mov   pllcon,#008h       ;16.777216MHz
;timer1 init
            mov   th0,#038h         ;65536-200= reload waarde
            mov   tl0,#038h         ;143us overflowtime!
       	    mov	  TMOD,#00000010b
            setb  tr0               ;Timer1 runmode
;DAC init
            mov   daccon,#11101101b
;interrupt init
            setb  ea                ;global interrupts on
            setb  et0               ;Timer0 interrupts on
		
		    

            mov   Fincl,#98h        ;50hz=1d4ch=7500d
            mov   Finch,#3ah        ;100Hz=3a98h=15000d
            
main:					
			
		
			ljmp  main




;interruptroutine wordt uitgevoerd met een frequentie van:
;16,777216MHz/(12*(65536-reload))=6.99050666kHz--> 143.051147us (bij 65536-200d reload)
;de interruptroutine duurt 28.61 us @16.777216MHz.
;Dit is de DDS routine, met een 20 bit accumulator waarvan de 8 hoogste bits worden gebruikt
;om de sinustabel aan te spreken.Het fase-increment is een 16 bit register.
timer_143us:
push  acc
            push  psw
            push  b
            mov   a,Fadderl
            add   a,Fincl
            mov   Fadderl,a
            mov   a,Fadderm
            addc  a,Finch
            mov   Fadderm,a
            mov   b,a
            mov   a,Fadderh
            addc  a,#0
            mov   Fadderh,a
            mov   a,b
            anl   a,#0f0h
            swap  a
            mov   b,a
            mov   a,Fadderh
            anl   a,#0fh
            swap  a
            add   a,b
            mov   dpl,a
            mov   dph,#03h
            mov   a,#0
            movc  a,@a+dptr
            mov   dac0l,a
            pop   b
            pop   psw
            pop   acc
            reti
            
; 256 stappen sinusgolf tabel
            org   0300h
sine:	  db	080h,083h,086h,089h,08ch,08fh,092h,095h,098h,09ch,09fh,0a2h,0a5h,0a8h,0abh,0aeh
	      db	0b0h,0b3h,0b6h,0b9h,0bch,0bfh,0c1h,0c4h,0c7h,0c9h,0cch,0ceh,0d1h,0d3h,0d5h,0d8h
	      db	0dah,0dch,0deh,0e0h,0e2h,0e4h,0e6h,0e8h,0eah,0ech,0edh,0efh,0f0h,0f2h,0f3h,0f5h
	      db	0f6h,0f7h,0f8h,0f9h,0fah,0fbh,0fch,0fch,0fdh,0feh,0feh,0ffh,0ffh,0ffh,0ffh,0ffh
	      db	0ffh,0ffh,0ffh,0ffh,0ffh,0ffh,0feh,0feh,0fdh,0fch,0fch,0fbh,0fah,0f9h,0f8h,0f7h
	      db	0f6h,0f5h,0f3h,0f2h,0f0h,0efh,0edh,0ech,0eah,0e8h,0e6h,0e4h,0e2h,0e0h,0deh,0dch
	      db	0dah,0d8h,0d5h,0d3h,0d1h,0ceh,0cch,0c9h,0c7h,0c4h,0c1h,0bfh,0bch,0b9h,0b6h,0b3h
	      db	0b0h,0aeh,0abh,0a8h,0a5h,0a2h,09fh,09ch,098h,095h,092h,08fh,08ch,089h,086h,083h
	      db	080h,07ch,079h,076h,073h,070h,06dh,06ah,067h,063h,060h,05dh,05ah,057h,054h,051h
	      db	04fh,04ch,049h,046h,043h,040h,03eh,03bh,038h,036h,033h,031h,02eh,02ch,02ah,027h
	      db	025h,023h,021h,01fh,01dh,01bh,019h,017h,015h,013h,012h,010h,00fh,00dh,00ch,00ah
	      db	009h,008h,007h,006h,005h,004h,003h,003h,002h,001h,001h,000h,000h,000h,000h,000h
	      db	000h,000h,000h,000h,000h,000h,001h,001h,002h,003h,003h,004h,005h,006h,007h,008h
	      db	009h,00ah,00ch,00dh,00fh,010h,012h,013h,015h,017h,019h,01bh,01dh,01fh,021h,023h
	      db	025h,027h,02ah,02ch,02eh,031h,033h,036h,038h,03bh,03eh,040h,043h,046h,049h,04ch
	      db	04fh,051h,054h,057h,05ah,05dh,060h,063h,067h,06ah,06dh,070h,073h,076h,079h,07ch



      end